---替换技能
---@class BuffEffect20 : BaseBuffEffect
BuffEffect20 = ClientFight.CreateClass("BuffEffect20", ClientFight.BaseBuffEffect)
local t = BuffEffect20
t.BUFF_EFFECT_PARAMETERS_20_ = "BUFF_EFFECT_PARAMETERS_20_"
---@param fight Fight
---@param buff FighterBuff
function t:add(fight, buff)
    local fighter = buff.source
    --替换的技能
    local skillId2 = buff.buffBean.f_BuffEffectValue[3]

    local sort = 0;
    if (buff.buffBean.f_BuffEffectValue[1] == 0) then
        --  //普攻
        local normalSkill = fighter.normalAttack
        if (self:replaceSkill(fight, fighter, normalSkill.skillId, skillId2)) then
            fighter.paramemters[t.BUFF_EFFECT_PARAMETERS_20_ .. buff.modelId] = tostring(normalSkill.skillId)
        end
    else
        --  //技能
        --被替换的技能(1级技能id)
        local beReplaceId = buff.buffBean.f_BuffEffectValue[2];
        local skillId1 = 0;
        local skills = fighter.skills
        for k, skill in pairs(skills) do
            if (skill.skillId / 1000 == beReplaceId / 1000) then
                skillId1 = skill.skillId
                break ;
            end
        end
        local replaceSkill = self:replaceSkill(fight, fighter, skillId1, skillId2);
        if (skillId1 > 0 and replaceSkill) then
            fighter.paramemters[t.BUFF_EFFECT_PARAMETERS_20_ + buff.modelId] = tostring(skillId1)
        end
        local flag = replaceSkill and 1 or 0
        sort = bit.bor(sort, flag)
        if (sort == 1) then
            -- 排序
            table.sort(fighter.skills, function(o1, o2)
                return o1.skillBean.f_SkillPriority > o2.skillBean.f_SkillPriority
            end)
        end
    end

    return ClientFight.BaseBuffEffect.add(self, fight, buff);
end
---@param fight Fight
---@param buff FighterBuff
function t:remove(fight, buff, force, ...)
    local fighter = buff.source

    --被替换的技能
    local skillId1 = buff.buffBean.f_BuffEffectValue[3];
    --换回来的技能
    local skillId2 = tonumber(fighter.paramemters[t.BUFF_EFFECT_PARAMETERS_20_ .. buff.modelId]) or 0
    if (skillId2 == 0) then
        return ClientFight.BaseBuffEffect.remove(self, fight, buff, force, ...);
    end
    local fightSkill2 = SkillManager.createFightSkill(fight, skillId2, fighter);
    if (fightSkill2 == nil) then
        return ClientFight.BaseBuffEffect.remove(self, fight, buff, force, ...);
    end
    if (buff.buffBean.f_BuffEffectValue[1] == 0) then
        fighter.normalAttack = fightSkill2
    else
        local skills = fighter.skills
        for k, s in pairs(skills) do
            if (s.skillId == skillId1) then
                skills[k] = fightSkill2
            end
        end
        table.sort(skills, function(o1, o2)
            return o1.skillBean.f_SkillPriority > o2.skillBean.f_SkillPriority
        end)
    end
    SkillRoleMgr.savePathFile("[".. fight.frame.. "][EFFECT]Fighter ".. fighter.fighterId.. " Buff"
   .. " 替换技能buff移除,换回技能(".. skillId2.. ")");
    return ClientFight.BaseBuffEffect.remove(self, fight, buff, force, ...);
end
-- 替换技能
--- @param skillModelId1 被替换技能
--- @param skillModelId2 替换技能
function t:replaceSkill(fight, fighter, skillModelId1, skillModelId2)
    local fightSkill2 = SkillClientManager.createFightSkill(fight, skillModelId2, fighter);
    if (fightSkill2 == null) then
        return false;
    end
    if (fighter.normalAttack.skillId == skillModelId1) then
        fighter.normalAttack = fightSkill2;
        SkillRoleMgr.savePathFile("[".. fight.frame.. "][EFFECT]Fighter ".. fighter.fighterId.. " Buff"
               .. " 替换技能(".. skillModelId1.. ")为(".. skillModelId2.. ")");
        return true;
    else
        local skills = fighter.skills;
        local size = table.getn(skills);
        for i = 1, size do
            local fightSkill = skills[i];
            if (fightSkill.skillId == skillModelId1) then
                skills[i] = nil;
                table.insert(skills, fightSkill2);
                SkillRoleMgr.savePathFile("[".. fight.frame.. "][EFFECT]Fighter ".. fighter.fighterId.. " Buff"
                       .. " 替换技能(".. skillModelId1.. ")为(".. skillModelId2.. ")");
                return true;
            end
        end
    end
    return false;
end

t.New()